package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/ordersetting")
@Api(tags = "预约设置管理接口")
public class OrderSettingController {
    @Autowired
    private OrderSettingService orderSettingService;
    /**
     * 模版下载
     */
    @GetMapping("/download")
    @ApiOperation("模版下载")
    public String download(@RequestParam String fileName, HttpServletResponse httpServletResponse) {
        log.info("模版下载");
        orderSettingService.download(fileName,httpServletResponse);
        return "文件下载";
    }
    /**
     * 根据日期修改可预约人数
     */
    @PostMapping("/editNumberByOrderDate")
    @ApiOperation("根据日期修改可预约人数")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting) {
        log.info("根据日期修改可预约人数");
        orderSettingService.editNumberByOrderDate(orderSetting);
        return new Result(true, MessageConstant.ORDER_SUCCESS,null);
    }
    /**
     * 根据年月查询对应的预约设置信息
     */
    @GetMapping("/getOrderSettingByMonth")
    @ApiOperation("根据年月查询对应的预约设置信息")
    public Result getOrderSettingByMonth(@RequestParam(required = false) String date) {
        log.info("根据年月查询对应的预约设置信息");
        List<OrderSetting> list = orderSettingService.getOrderSettingByMonth(date);
        try {
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }
    /**
     * 文件上传Excel
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传Excel")
    public Result upload(@RequestBody MultipartFile excelFile) {
        log.info("文件上传Excel");
        try {
            orderSettingService.upload(excelFile);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }
}
